#!/usr/bin/env bash
. `dirname "$0"`/sh/init.sh

cd $ROOT
if [ ! \( -e "./public/6.js" \) ]; then
  print "init public"

  dirpath=public/.cache/git/github.com/6du-plugin
  mkdir -p $dirpath
  plugin=../plugin

  for D in $plugin/*; do
    rm -rf $dirpath/`basename $D`
    ln -s ../../../../../$D $dirpath/`basename $D`
  done

  if [ ! \( -e "$ROOT/../sh/node_modules" \) ]; then
    cd $ROOT/../sh
    npx yarn
    npx yarn link
  fi

  cd $ROOT/../template
  git add --update :/
  git commit -m "-"

  cd $ROOT/public
  6du -y --cwd .
  rm -rf md li pug

  site=../../template
  for D in $site//*; do
    if [ ! \( -e `basename $D` \) ]; then
      ln -s $D .
    fi
  done

  cd 6du
  for D in ../$site/6du/*; do
    rm -rf `basename $D`
    ln -s $D .
  done
  rm -rf 6.js
  ln -s ../../sh/6.js .
  cd ..
  rm -rf sw.js
  ln -s ../../template/6du/sw.js .
  6du
  cd ../../template
  git checkout .
fi

cd $ROOT
npx yarn run serve

